<template>
  <by-background />

  <view class="padding-bottom-20">
    <by-navigation-bar title="技师之家" :showBack="false" >
      <template #ending>
        <by-view @click="showMenu = true">
          <image src="/static/menu.png" class="menu" />
        </by-view>
      </template>
    </by-navigation-bar>

    <view class="padding-lr-20">
      <template v-if="list.length">
        <by-list-card v-for="item in list" :key="item.uid" :item="item" :show-contact="isShowContact" @showSafeguard="showSafeguard = true" @touch="showContact($event, item.tno)" />
        <by-load-footer :has-more="hasMore" text="加载中..." />
      </template>
      <view v-else-if="!(userStore.location.lat && userStore.location.lng)" class="flex-column flex-center request-location">
        <text class="text-df margin-bottom-10">为你匹配附近的技师</text>
        <text class="text-df margin-bottom-30">请授权我们获取此设备的位置权限</text>
        <by-button width="50vw" text="确认" @click="checkOpenGPSServiceByAndroid" />
      </view>
    </view>
  </view>

  <by-page-dialogs
    v-model:show-touch="showTouch"
    v-model:show-contact-way="showContactWay"
    v-model:show-error="showError"
    v-model:show-safeguard="showSafeguard"
    :point="point"
    :tno="tno"
    @showContact="showContactWay = true"
  />

  <mine-menu v-model="showMenu" />

  <by-dialog v-model="showAccredit" title="为你匹配附近的技师" content="请授权我们获取此设备的位置权限?" @confirm="handleGetLocation" />

  <!-- #ifdef APP-PLUS -->
  <by-app-update v-model="showUpdateDialog" ref="updateDialog" @getVersion="getNewVersion"/>
  <!-- #endif -->
</template>

<script setup lang="ts">
import MineMenu from './mine-menu.vue'
import useUserStore from '@/store/user'
import { getHonorAuditConfig, getTechnician, updatelocation } from '@/common/api'
import { GetList, getLocation, checkOpenGPSServiceByAndroid } from '@/common/util'
import type { AnyObject } from '@/common/types'

const userStore = useUserStore()
const showTouch = ref(false)
const showContactWay = ref(false)
const showError = ref(false)
const showSafeguard = ref(false)
const showMenu = ref(false)
const point = ref(0)
const tno = ref('')
const updateDialog = ref()
const showAccredit = ref(!(userStore.location.lat && userStore.location.lng))

/* onMounted(() => {
  const isNFirst = uni.getStorageSync('first-open-technician')
  if (isNFirst) {
    handleGetLocation()
  } else {
    // showAccredit.value = true
    uni.setStorageSync('first-open-technician', 1)
  }
})*/

function showContact(e:AnyObject, id:string) {
  tno.value = id
  if (e.unlock) {
    showContactWay.value = true
  } else {
    point.value = e.unlockPoint
    if (e.canGet) showTouch.value = true
    else showError.value = true
  }
}

/**
 * 获取列表 start
 * */
const hasMore = ref(true)
const list = shallowRef<AnyObject[]>([])
const params = reactive({
  ...userStore.location,
  distance: '',
  noLoading: true
})
const getList = new GetList<AnyObject, AnyObject>(getTechnician, params)
async function reload() {
  Object.assign(getList.params, userStore.location)
  list.value = await getList.reload()
  hasMore.value = getList._has_more
}
async function nextPage() {
  hasMore.value = getList._has_more
  list.value.push(...await getList.nextPage())
  triggerRef(list)
}
onPullDownRefresh(handleGetLocation)
onReachBottom(nextPage)
/**
 * 获取列表 end
 * */

/**
 * 获取地理位置 end
 * */
function getNewVersion() {
  setTimeout(updateDialog.value.handleNewGetVersion, 1500)
}
async function handleGetLocation() {
  showAccredit.value = false
  /* #ifdef APP-PLUS*/
  handleGetAppleAuditConfig()
  /* #endif*/
  const res = await getLocation()
  /* userStore.location = {
    lng: '116.32247450',
    lat: '39.63087782'
  }*/
  userStore.location = res
  if (userStore.token) await updateToken()
  reload()
}
if (userStore.location.lat && userStore.location.lng) handleGetLocation()

async function updateToken() {
  const res = await updatelocation(userStore.location)
  userStore.userData = res.data
}
/**
 * 获取地理位置 end
 * */

/**
  * 返回监听 start
  * */
const showUpdateDialog = ref(false)
const back = computed(() => showTouch.value || showContactWay.value || showError.value || showSafeguard.value || showMenu.value || showUpdateDialog.value || showAccredit.value)
onBackPress(() => {
  if (back.value) {
    close()
    return true
  }
})
function close() {
  showTouch.value = showContactWay.value = showError.value = showSafeguard.value = showMenu.value = showUpdateDialog.value = showAccredit.value = false
}
/**
 * 返回监听 end
 * */

const isShowContact = ref(true)
/* #ifdef APP-PLUS*/
import useSystemStore from '@/store/system'
import { getAppleAuditConfig } from '@/common/api'
const systemStore = useSystemStore()
async function handleGetAppleAuditConfig() {
  const res = await getAppleAuditConfig()
  const { isAppleAudit } = res.data
  if (systemStore.systemInfo.osName === 'ios') userStore.canPay = !isAppleAudit
}

async function handleGetHonorAuditConfig() {
  const res = await getHonorAuditConfig()
  const { isAudit } = res.data
  if (systemStore.systemInfo.brand === 'honor' || systemStore.systemInfo.brand === 'huawei' || systemStore.systemInfo.brand === 'vivo') isShowContact.value = !isAudit
}
handleGetHonorAuditConfig()
/* #endif*/
</script>

<style lang="scss">
.menu{
  width: 40rpx;
  height: 36rpx;
  display: block;
}
.request-location{
  padding-top: 40vh;
}
</style>
